import {Queue} from "../Framework/Utils/Queue";
import {LogTool} from "../Framework/Utils/LogTool";
import {UIManager} from "../Framework/Managers/UIManager";
import TestUI from "./UI/TestUI";
import { ResManager, RetHander, SaveType } from "../Framework/Managers/ResManager";
import { ConstValue } from "../Framework/ConstType/ConstValue";
import { ConfigManager } from "../Framework/Managers/ConfigManager";
import { SkillConfig } from "../Framework/Config/SkillConfig";
import { resLoader } from "../Framework/res/ResLoader";
import ResUtil from "../Framework/res/ResUtil";

const {ccclass, property} = cc._decorator;
@ccclass
export default class Test extends cc.Component {
    @property(cc.Node)
    private goto_btn:cc.Node=null;

    @property(cc.ScrollView)
    list:cc.ScrollView=null;
    @property(cc.Node)
    cell:cc.Node=null;
    @property
    _cellCount:number=0;
    _list:Queue<any> = new Queue<any>();
    onLoad(){
        interface bird{
            fly();
            layeggs();
        }
        interface fish{
            swim();
            layeggs();
        }
    }
    start(){
        this.initCanvas();

        // let checker = new ResLeakChecker();
        // checker.startCheck();
        // resLoader.resLeakChecker = checker;

        // let cellSize=this.cell.getContentSize();
        // this.cell.active=false;
        // let c_count=[];
        // this._cellCount=100;
        // var hander=function(name,list,index){
        //     // cc.log(name,list,index);
        //     switch(name){
        //         case 'count':
        //             {
        //                 return this._cellCount;
        //             }
        //         case 'cellSize':
        //             {
        //                 return cellSize;
        //             }
        //         case 'cell':
        //             {
        //                 let cell=null;
        //                 if(this._list.empty()){
        //                     cell=cc.instantiate(this.cell);
        //                     c_count.push(cell);
        //                 }
        //                 else{
        //                     cell= this._list.pop();
        //                 }
        //                 cell.active=true;
        //                 cell.getComponent(cc.Component).showInfo(index);
        //                 return cell;
        //             }
        //         case 'removeCell':
        //             {
        //                 if(index){
        //                     index.active=false;
        //                     this._list.push(index);
        //                 }
        //             }
        //             break;
        //     }
        // }.bind(this);

       // let view = this.list.getComponent('TableView') as TableView;
       // view.setScrollView(this.list);
       // view.setHandler(hander);
       // view.reloadData();

        // LogTool.log(454545,7878);
        // ConfigManager.getInstance().loadAllConfig([SkillConfig]);
        // resLoader.loadResDir("prefab/ui/TestUI", cc.Prefab, (error: Error, prefabs: cc.Prefab[]) => {
        //     console.log(error,prefabs)
        //     if (!error) {
        //         for (let i = 0; i < prefabs.length; ++i) {
        //             let view= ResUtil.instantiate(prefabs[i]);
        //             view.parent = cc.find('Canvas');
        //             console.log(view.getComponent(cc.Component));
        //             view.getComponent(cc.Component).autoReleaseRes({ url: resLoader.getResKeyByUrl("prefab/ui/TestUI", cc.Prefab), type: cc.Prefab, use: 'test' }, false);
        //         }
        //     }
        // }, "test");
        ResManager.getInstance().loadRes('http://tools.itharbors.com/christmas/res/tree.png',cc.Asset,function(ret:RetHander){
            console.log(ret.m_data)
            let spriteFrame = new cc.SpriteFrame(ret.m_data[0]);
            let node = new cc.Node("tmp");
            let sprite = node.addComponent(cc.Sprite);
            sprite.spriteFrame = spriteFrame;
            node.parent = cc.find('Canvas');
            console.log(node);
        },{saveType:SaveType.Asset});
        // resLoader.loadRes("http://tools.itharbors.com/christmas/res/tree.png", (err, res) => {
        //     if (err || !res) return;
        //     let spriteFrame = new cc.SpriteFrame(res);
        //     let node = new cc.Node("tmp");
        //     let sprite = node.addComponent(cc.Sprite);
        //     sprite.spriteFrame = spriteFrame;
        //     node.parent = cc.find('Canvas');
        // }, "net")
        this.goto_btn=this.node.getChildByName('goto_btn');
        this.goto_btn.on(cc.Node.EventType.TOUCH_START,function () {
            // LogTool.log2(this,7777);
            // UIManager.getInstance().showUI(TestUI,(ui:TestUI,arg:any[])=>{
            //     LogTool.log2(this,7878);
            // });
            // this._cellCount=60;
            // view.reloadData(true);
            // cc.find('Canvas').removeAllChildren(true);
            // resLoader.releaseResDir("prefab/ui/TestUI",cc.Prefab,"test");
            resLoader.releaseRes("http://tools.itharbors.com/christmas/res/tree.png", "net");
        },this);

        // for(var i=0;i<10;i++){
        //     let temp =i;
        //     ResManager.getInstance().addLoadingResItem('prefab/ui/TestUI',cc.Prefab,function(ret:RetHander){
        //         console.log(ret);
        //     },{count:5,saveType:SaveType.Item,argArray:[1,1,1,1,4,4]});
        // }

        // ConfigManager.getInstance().loadAllConfig();

        // ResManager.getInstance().loadRes(ConstValue.config_dir+'SkillConfig.txt',cc.Asset,function(ret:RetHander){
        //     cc.log(ret.m_data);
        // });

        // UIManager.getInstance().openUI(TestUI,0,(ui:TestUI,arg:any[])=>{
        //     cc.log(ui,arg);
        //     LogTool.log('ui,arg');
        // },(completedCount: number, totalCount: number, item: any)=>{
        //     cc.log(completedCount,totalCount,item);
        // });
    }
    initCanvas(){
        let frameSize = cc.view.getFrameSize();
        let bFitWidth = (frameSize.width / frameSize.height) < (750 / 1334);
        cc.Canvas.instance.fitWidth = bFitWidth;
        cc.Canvas.instance.fitHeight = !bFitWidth;
    }
}
